################################################################################
# File: manageWS.py
# Package: itacqos.forms
# Author: Vincent Pretre
# Last update: $LastChangedDate$
################################################################################

## @package itacqos.uddi.forms
#
# This module contains declaration of all forms used in iTaC-QoS.

from django import forms
from django.forms import ModelForm
from django.contrib.auth.models import User

from uddi.models import Vendor, WebService

## This class describes a form used when registering.
class RegisterForm(forms.Form):
	## TextField used to set login.
	username = forms.CharField(label = 'login')
	## TextField used to set email.
	email = forms.EmailField(label = 'e-mail')
	## TextField used to set password.
	password = forms.CharField(widget=forms.PasswordInput, label = 'password')
	## This textField is used to check password.
	# The user has to enter its password twice to be sure that no misstake has been done.
	passwordCheck = forms.CharField(widget=forms.PasswordInput, label = 'please re-type')
	## Checkbox used to distinct vendor and simple users.
	isVendor = forms.BooleanField(label = 'Are you a web service vendor ?',  required=False)

	## This function is used to validate the form.
	# @param self Pointer to the object
	#@return Boolean 
	#
	# This function uses the "is_valid()" form method to validate each field individually. Then, it checks if the login
	# is not already used and if the two password are equal.
	def validate(self):
		res = True
		if self.is_valid():
			if User.objects.filter(username__exact=self.cleaned_data['username']).count() == 1:
				self.errors['username'] = ['This login is already used']
				res = False
			if self.cleaned_data['password'] != self.cleaned_data['passwordCheck']:
				self.errors['passwordCheck'] = ['Password missmatch']
				res = False
		else:
			res = False
		return res

## This class describes a form used to update user's information (email and password)
#
# The class RegisterForm can not be used because it has controls differ. With this field, the user can not change his login and has the 
#choice to keep his old password.
class UpdateUserForm(forms.Form):
	## TextField used to update e-mail
	email = forms.EmailField(label = 'e-mail')
	## TextField used to check that the user knows his current password.
	# We need to check this to avoid some problems, such as a non-closed session in a public place etc.
	oldPassword = forms.CharField(widget=forms.PasswordInput, label = 'current password', required = False)    
	## TextField used to set the new password
	password = forms.CharField(widget=forms.PasswordInput, label = 'new password', required = False)
	## TextField used to check new password validity.
	passwordCheck = forms.CharField(widget=forms.PasswordInput, label = 'please re-type', required = False)

	## This function is used to validate the form.
	#
	# This function uses the "is_valid()" form method to validate each field individually. Then it checks if the old password is the right one
	# and if the two new passwords are equal and not empty.
	def validate(self, user):
		res = True
		if self.is_valid():
			if self.cleaned_data['oldPassword'] != '':
				if not user.check_password(self.cleaned_data['oldPassword']):
					self.errors['oldPassword'] = ['Wrong password']
					res = False
				elif self.cleaned_data['password'] != self.cleaned_data['passwordCheck']:
					self.errors['passwordCheck'] = ['Password missmatch']
					res = False
				elif self.cleaned_data['password'] == '':
					self.errors['password'] = ['Password can not be empty']
					res = False
		else:
			res = False
		return res


## This class describes a form to create/update public informations of vendors
#
# Those informations are :
# - displayed name
# - description
# - website
# This form is automatically generated from the Vendor class. Only one information can not be updated: the user 
# linked to the vendor.
class VendorForm(ModelForm):
	class Meta:
		model = Vendor
		exclude = ('user')

## This class is used to create/update a web service.
#
# This form is automatically generated from the WebService class. Some informations of a web service can
# not be updated:
# - the vendor
# - the folder containing files about the service (WSDL file, XMI model, tests ...)
# - the percentage of successful tests (of course :D ). Tests linked to the service can not be changed with this form
class ServiceForm(ModelForm):
	## A TextField used to set keywords linked to the web service.
	# We chose to use a TextField because it is more simple to use than a "select" list, in particular when there is a lot
	# of keywords. It is also more simple to add new keywords this way.
	kw = forms.CharField(max_length = 100,  label = 'Keywords', help_text = 'Split keywords with comma')
	## A file used to upload XMI model.
	xmiModel = forms.FileField(label = 'XMI model')

	class Meta:
		model = WebService
		exclude = ('producedBy', 'keywords', 'dir', 'percentageOfSuccessfulTests')
